package com.intretech.umsin.oss.service.impl;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.intretech.umsin.oss.config.FastDfsOSSConfig;
import com.intretech.umsin.oss.entity.FileType;
import com.intretech.umsin.oss.service.MetadataStoreService;
import com.intretech.umsin.oss.vo.FileInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @version V1.0
 * @ClassName: FastDfsOssServiceImpl
 * @Description: FastDfsOssServiceImpl fastdfs 文件存储服务实现
 * @author: 李启联
 * @date: 2022-04-25 23:09
 * @Copyright: Copyright (c) 2022
 */
@Service
@Slf4j
public class FastDfsOssServiceImpl extends AbstractFileService{

    @Autowired(required = false)
    private MetadataStoreService metadataStoreService;

    @Autowired(required = false)
    private FastDfsOSSConfig fastDfsOSSConfig;

    @Resource
    private FastFileStorageClient storageClient ;

    /**
     * OSS存储类型
     *
     * @return
     */
    @Override
    public FileType fileType() {
        return FileType.SFTP;
    }

    /**
     * 文件元数据存储服务
     *
     * @return
     */
    @Override
    protected MetadataStoreService getMetadataStoreService() {
        return metadataStoreService;
    }

    /**
     * 上传文件
     *
     * @param file
     * @param fileInfo
     */
    @Override
    protected void uploadFile(MultipartFile file, FileInfoVo fileInfo) throws Exception {
        String originalFilename = file.getOriginalFilename().
                substring(file.getOriginalFilename().
                        lastIndexOf(".") + 1);
        StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
                file.getInputStream(),
                file.getSize(),originalFilename , null);
        fileInfo.setTagsName(storePath.getFullPath());
    }

    /**
     * 删除文件资源
     *
     * @param fileInfo
     * @return
     */
    @Override
    protected boolean deleteFile(FileInfoVo fileInfo) {
        String fullPath = fileInfo.getPath();
        if(fullPath.startsWith("/")){
            fullPath = fullPath.substring(1);
        }
        if (StringUtils.isEmpty(fullPath)) {
            return false;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(fullPath);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 对查询返回的文件列表设置其对应文件访问的URL
     *
     * @param fileInfo
     */
    @Override
    protected void setFileUrl(FileInfoVo fileInfo) {
        fileInfo.setUrl(fastDfsOSSConfig.getDomain() + fileInfo.getPath());
    }

    /**
     * 下载文件
     *
     * @param fileInfo
     * @return
     */
    @Override
    public byte[] downloadFile(FileInfoVo fileInfo) throws Exception {
        try {
            String filePathName = fileInfo.getPath();
            String fullPath = "", fileName = filePathName;
            if (filePathName.lastIndexOf("/") > 0) {
                if(filePathName.startsWith("/")) {
                    fullPath = filePathName.substring(1);
                }else{
                    fullPath = filePathName;
                }
                fileName = filePathName.substring(filePathName.lastIndexOf("/") + 1);
            }
            String group = fullPath.substring(0, fullPath.indexOf("/"));
            String path = fullPath.substring(fullPath.indexOf("/") + 1);
            DownloadByteArray downloadByteArray = new DownloadByteArray();
            byte[] bytes = this.storageClient.downloadFile(group, path, downloadByteArray);

            return bytes;

        } catch (Exception e) {
            log.error("download file error!", e);
        } finally {

        }
        return null;
    }

}
